package com.liu.webstore.util;

import com.liu.webstore.entity.User;
import org.springframework.context.annotation.Configuration;

/**
 * 持有用户信息，用户代替session对象
 * 考虑线程隔离情况，使用ThreadLocal，以线程为key存取value
 *
 * @Author LiuYumei
 * @Date 2022/7/23 10:23
 */
@Configuration
public class HostHolder {
    private ThreadLocal<User> users = new ThreadLocal<>();

    public void setUser(User user) {
        users.set(user);
    }

    public User getUser() {
        return users.get();
    }

    /**
     * 请求结束后把用户信息清除
     */
    public void clear() {
        users.remove();
    }
}
